package com.itheima.store.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import com.itheima.store.dao.DeptmanagerDao;

import com.itheima.store.domain.Dept;
import com.itheima.store.utils.JDBCUtils;
/**
 * 分类的DAO的实现类
 * @author admin
 *
 */
public class DeptmanagerDaoImpl implements DeptmanagerDao {

	@Override
	public List<Dept> findAllDept() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from dept";
		List<Dept> list = queryRunner.query(sql, new BeanListHandler<Dept>(Dept.class));
		return list;
	}

	@Override
	public void saveDept(Dept dept) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql ="insert into dept(dept_id,dept_name) values(?,?)";
		int i = queryRunner.update(sql, dept.getDept_id(),dept.getDept_name());
		
	}

	@Override
	public Dept queryDept(String dept_id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from dept where dept_id=?";
		Dept dept = queryRunner.query(sql, new BeanHandler<Dept>(Dept.class), dept_id);
		return dept;
	}

	@Override
	public void updateDept(Dept dept) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update dept set dept_name =? where dept_id=?";
		queryRunner.update(sql, dept.getDept_name(),dept.getDept_id());
	}

	@Override
	public void deleteDept(String dept_id)throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "delete from dept where dept_id =?";
		queryRunner.update(sql, dept_id);
		
	}

}
